// pages/scratchlist/scratchlist.js
const {
  imgConfig,
  config
} = require('../../utils/config.js')
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    stuId: 0,
    pageIndex: 1, //页码
    pageSize: 7, //页面数据大小
    workList:''
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.getWorkList()
    if (app.globalData.userInfo != null) {
      this.setData({
        stuId: app.globalData.userInfo.id
      })
    }
  },
  goodCurrent(e) {
    console.log(e)
    var that = this
    var index = e.target.dataset.index
    var workList = that.data.workList
    var work = that.data.workList[index]
    console.log(work)
    var status = true
    if(work.status == true){
      var goodNum = work.goodNum -1
      workList[index].status = false 
      workList[index].goodNum -= 1
    }else {
      var goodNum = work.goodNum +1
      workList[index].status = true 
      workList[index].goodNum += 1
      status = false
    }
    that.setData({
      workList:workList
    })
      wx.request({
        url: `${config}/scratch/erpscratchuploadrecord/updateGood`,
        data:{
          id:work.id,
          goodNum:goodNum,
          goodId:work.goodId,
          status:status,
          stuId:app.globalData.userInfo.id
        },
        success(res){
          console.log(res)
          if(res.flag == true){
          
          }
        }
      
      })
    
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },
  getWorkList() {
    var that = this
    wx.request({
      url: `${config}/scratch/erpscratchuploadrecord/getWorkList`,
      data: {
        deptId: app.globalData.deptId,
        stuId:app.globalData.userInfo.id,
        page: that.data.pageIndex,
        limit: that.data.pageSize,
      },
      success(res) {
        var workList = res.data.page.list
        that.setData({
          workList: workList
        })
      }
    })
  },
  goScratchView(e) {
    var that = this
    var index = e.currentTarget.dataset.index
    var previewPath = that.data.workList[index].previewPath
    wx.navigateTo({
      url: '/pages/scratch/scratch?previewPath=' + encodeURIComponent(previewPath),
    })
  },
  addShareNum(e) {
    console.log(e)
    wx.request({
      url: `${config}/scratch/erpscratchuploadrecord/addShareNum`,
      method: 'post',
      data: {
        id: e.id,
        shareNum: e.shareNum
      },
      success(res) {
        console.log(res)
      }
    })
  },
  onShareAppMessage(e) {
    var that = this
    var index = e.target.dataset.index
    var work = that.data.workList[index]
    console.log(work)
    var previewPath = that.data.workList[index].previewPath
    that.addShareNum(work)
    return {
      title: '分享',
      path: '/pages/scratch/scratch?previewPath=' + encodeURIComponent(previewPath),
      imageUrl: 'https://hqzy-erp.oss-cn-hangzhou.aliyuncs.com/codingmore/banner1.png',
    }


  }

})